package com.xbongbong.paas.help.subform;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.saas.domain.entity.ProductWarehouseEntity;
import com.xbongbong.saas.enums.subform.ProductWarehouseSubFormEnum;
import com.xbongbong.paas.help.subform.abstracts.AbstractTransferSubForm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author 魏荣杰
 * @date 2020/8/25 10:35
 * @since
 * @version
 */
@Component
public class ProductWarehouseTransferSubForm extends AbstractTransferSubForm {

    private static final Logger LOG = LoggerFactory.getLogger(ProductWarehouseTransferSubForm.class);


    public PaasFormDataEntity transferEntityToFormData(ProductWarehouseEntity productWarehouseEntity) {
        PaasFormDataEntity paasFormDataEntity = new PaasFormDataEntity();
        paasFormDataEntity.setId(productWarehouseEntity.getId());
        paasFormDataEntity.setCorpid(productWarehouseEntity.getCorpid());
        paasFormDataEntity.setDel(productWarehouseEntity.getDel());
        paasFormDataEntity.setAddTime(productWarehouseEntity.getAddTime());
        paasFormDataEntity.setUpdateTime(productWarehouseEntity.getUpdateTime());

        JSONObject dbJson = JSONObject.parseObject(JSON.toJSONString(productWarehouseEntity));

        JSONObject data = new JSONObject();
        ProductWarehouseSubFormEnum[] values = ProductWarehouseSubFormEnum.values();
        for (ProductWarehouseSubFormEnum value : values) {
            String dbAttr = value.getDbAttr();
            String dataAttr = value.getDataAttr();
            Integer fieldType = value.getFieldType();
            formatDataForEs(dbJson, data, dbAttr, dataAttr, fieldType);
        }
        paasFormDataEntity.setData(data);

        return paasFormDataEntity;
    }

    public List<PaasFormDataEntity> transferEntityToFormDataList(List<ProductWarehouseEntity> productWarehouseEntities) {
        List<PaasFormDataEntity> paasFormDataEntities = new ArrayList<>();
        for (ProductWarehouseEntity productWarehouseEntity : productWarehouseEntities) {
            PaasFormDataEntity paasFormDataEntity = transferEntityToFormData(productWarehouseEntity);
            paasFormDataEntities.add(paasFormDataEntity);
        }
        return paasFormDataEntities;
    }

    public ProductWarehouseEntity transferFormDataToEntity(PaasFormDataEntity paasFormDataEntity) {

        JSONObject data = paasFormDataEntity.getData();

        JSONObject dbData = new JSONObject();
        ProductWarehouseSubFormEnum[] productWarehouseSubFormEnums = ProductWarehouseSubFormEnum.values();
        for (ProductWarehouseSubFormEnum productWarehouseSubFormEnum : productWarehouseSubFormEnums) {
            String dbAttr = productWarehouseSubFormEnum.getDbAttr();
            String dataAttr = productWarehouseSubFormEnum.getDataAttr();
            Integer fieldType = productWarehouseSubFormEnum.getFieldType();
            formatDataForDb(data, dbData, dbAttr, dataAttr, fieldType);
        }
        ProductWarehouseEntity productWarehouseEntity = JSON.parseObject(dbData.toJSONString(), ProductWarehouseEntity.class);
        productWarehouseEntity.setAddTime(paasFormDataEntity.getAddTime());
        productWarehouseEntity.setUpdateTime(paasFormDataEntity.getUpdateTime());
        productWarehouseEntity.setCorpid(paasFormDataEntity.getCorpid());
        productWarehouseEntity.setDel(paasFormDataEntity.getDel());
        productWarehouseEntity.setId(paasFormDataEntity.getId());

        return productWarehouseEntity;
    }


    public List<ProductWarehouseEntity> transferFormDataToEntityList(List<PaasFormDataEntity> paasFormDataEntities) {
        List<ProductWarehouseEntity> productWarehouseEntities = new ArrayList<>();
        for (PaasFormDataEntity paasFormDataEntity : paasFormDataEntities) {
            ProductWarehouseEntity productWarehouseEntity = transferFormDataToEntity(paasFormDataEntity);
            productWarehouseEntities.add(productWarehouseEntity);
        }
        return productWarehouseEntities;

    }
}
